package com.huaxin.common.media.view;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.huaxin.common.media.adapter.PhotoAdapter;
import com.huaxin.common.media.bean.Image;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2016/10/17.
 */
public class PhotoAddView extends RecyclerView {
    private Context mContext;
    private List<String> mDatas = new ArrayList<>();
    private PhotoAdapter photoAdapter;
    private List<String> mCompress = new ArrayList<>();
    private int          maxSize   = 4;
    private List<Image> mSelectedImage;
    private boolean     isRecorderVideo;
    private boolean     isVideo;

    public PhotoAddView(Context context) {
        super(context);
        mContext = context;
        mSelectedImage = new ArrayList<>();
        init();
    }

    public PhotoAddView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public PhotoAddView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        photoAdapter = new PhotoAdapter(mContext, mDatas, mSelectedImage);
        setLayoutManager(new GridLayoutManager(mContext, 4));
        photoAdapter.setDatas(mDatas, mCompress);
        photoAdapter.setMaxSize(maxSize);
        photoAdapter.isRecorderVideo(isRecorderVideo);
        setAdapter(photoAdapter);
    }

    //    public void setDatas(List<String> datas,List<String> compress) {
    //        this.mDatas = datas;
    //        this.mCompress = compress;
    //    }

    public void setDatas(String[] imgList, String[] compress) {
        mDatas.clear();
        if (imgList != null) {
            mDatas.addAll(Arrays.asList(imgList));
        }
        mCompress.clear();
        if (imgList != null) {
            mCompress.addAll(Arrays.asList(compress));
        }

    }

    public String[] getDatas() {
        return mDatas.toArray(new String[mDatas.size()]);
    }

    public String[] getCompressPaths() {
        return mDatas.toArray(new String[mDatas.size()]);
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
        if (photoAdapter != null) {
            photoAdapter.setMaxSize(maxSize);
        }
    }

    public void isRecorderVideo(boolean isRecorderVideo) {
        this.isRecorderVideo = isRecorderVideo;
        if (photoAdapter != null) {
            photoAdapter.isRecorderVideo(isRecorderVideo);
        }
    }

    public boolean isVideo() {
        if (photoAdapter != null) {
            this.isVideo = photoAdapter.isVidoe();
        }
        return this.isVideo;
    }
}
